package com.carpool.finder;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.slidingmenu.lib.SlidingMenuActivity;

public class ViewRouteActivity extends SlidingMenuActivity{
	
	ImageView profile;
	TextView tvname;
	TextView tvroute;
	
	HashMap<String,String> user;
	DatabaseHandler db;
	Button btnMap;
	Button btnMap2;
	Button btnMap3;
	Intent mapIntent;
	Button logout;
	//JSON Response code names
	
	 private static String KEY_SUCCESS = "success";
	 private static String KEY_ERROR = "error";
	 private static String KEY_ERROR_MSG = "error_msg";
	 private static String KEY_NID = "uid";
	 private static String KEY_SNAME = "src_name";
	 private static String KEY_DNAME = "des_name";
	 private static String KEY_VNAME = "via_name";
	 ImageFinder im = new ImageFinder();
	//Menu Buttons
		Button home;
		Button search;
		Button route;
		String uid;
		Button delete;
		@Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.viewroute);
	        setBehindContentView(R.layout.menu);
	        setBehindOffset((int)getResources().getDimension(R.dimen.actionbar_home_width));
			setBehindScrollScale(0.5f);
			db=new DatabaseHandler(getApplicationContext());
			home = (Button)findViewById(R.id.btn_home);
			search = (Button)findViewById(R.id.btn_search);
			route = (Button)findViewById(R.id.btn_route);
			tvname = (TextView)findViewById(R.id.name);
			logout = (Button)findViewById(R.id.logout_but);
			tvroute = (TextView)findViewById(R.id.route_tag);
			delete = (Button)findViewById(R.id.delete_route);
			user = new HashMap<String,String>();
			profile = (ImageView)findViewById(R.id.profile_pic);
			user=db.getUserDetails();
			String name = user.get("name");
			String email = user.get("email");
			uid = user.get("uid");
			Log.e("UID :", uid);
			//String phone = user.get("phone");
			String image_url = user.get("image");
			tvname.setText(""+name);
			Bitmap bitmap = im.findImage(image_url);
			profile.setImageBitmap(bitmap);
			if (android.os.Build.VERSION.SDK_INT > 9) {
			      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
			      StrictMode.setThreadPolicy(policy);
			}
			
			user=db.getUserDetails();
			uid = user.get("uid");
			UserFunctions userFunction = new UserFunctions();
			JSONObject json = userFunction.getRoute(uid);
			try {
                if (json.getString(KEY_SUCCESS) != null) {
                    //registerErrorMsg.setText("");
                    String res = json.getString(KEY_SUCCESS);
                    if(Integer.parseInt(res) == 1){
                        JSONObject json_user = json.getJSONObject("route");
                        tvroute.setText(""+json_user.getString(KEY_SNAME)+"--->"+json_user.getString(KEY_VNAME)+"---->"+json_user.getString(KEY_DNAME));
                    }else{
                    	Toast.makeText(getBaseContext(), "No routes found!!", Toast.LENGTH_SHORT).show();
                    	Intent i = new Intent(getApplicationContext(), RouteActivity.class);
    					startActivity(i);
    					overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);
    					finish();
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
			
			delete.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					UserFunctions userFunction = new UserFunctions();
					JSONObject json = userFunction.deleteRoute(uid);
					try {
		                if (json.getString(KEY_SUCCESS) != null) {
		                    //registerErrorMsg.setText("");
		                    String res = json.getString(KEY_SUCCESS);
		                    if(Integer.parseInt(res) == 1){
		                    	Toast.makeText(getBaseContext(), "Deleted!!", Toast.LENGTH_SHORT).show();
		                    }else{
		                    	
		                    	Toast.makeText(getBaseContext(), "No such route registered!!", Toast.LENGTH_SHORT).show();
		                    }
		                }
		            } catch (JSONException e) {
		                e.printStackTrace();
		            }
					Intent i = new Intent(getApplicationContext(), HomeActivity.class);
					startActivity(i);
					overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);
					finish();
				}
			});
			
			route.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent i = new Intent(getApplicationContext(), ViewRouteActivity.class);
					startActivity(i);
					overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);
					
				}
			});
			
			search.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					
				}
			});
			
			logout.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent i = new Intent(getApplicationContext(), LoginActivity.class);
					i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(i);
					overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);
				}
			});
			
			home.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent i = new Intent(getApplicationContext(), HomeActivity.class);
					startActivity(i);
					overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);
					finish();
					
				}
			});
		}

}
